﻿/**
 * 请不要将该类库用于非法途径和商业途径，与此产生的一切法律后果与本人无关。
 * 有任何问题，可以访问我的博客：http://baiyafeng.com 留言
 * 使用本类库请遵循GPL v3协议。
 **/

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Reflection;
using System.Text;

namespace SinaBlogQiangShafa
{
    public class SerializationHelper
    {
        private static string SerializeObject(object obj)
        {
            if (null == obj)
                return string.Empty;
            Type type = obj.GetType();
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
            StringBuilder objString = new StringBuilder();
            foreach (FieldInfo field in fields)
            {
                object value = field.GetValue(obj);     //取得字段的值
                objString.Append(field.Name + ":" + value + ";");
            }
            return objString.ToString();
        }
        public static bool CompareObject(object obj1, object obj2)
        {
            return SerializeObject(obj1).Equals(SerializeObject(obj2));
        }
        private SerializationHelper()
        {

        }

        public static byte[] GetSerializationBytes(object obj)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, obj);
            byte[] message = stream.ToArray();
            stream.Close();

            return message;
        }

        public static object GetDeserializationObject(byte[] dataByte)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            using (MemoryStream stream = new MemoryStream(dataByte))
            {
                return formatter.Deserialize(stream);
            }
        }

        public static void Serialize(object data, string filePath)
        {
            try
            {
             
                StreamWriter fs = new StreamWriter(filePath, false);

                try
                {
                    MemoryStream streamMemory = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();

                   
                    formatter.Serialize(streamMemory, data);

                     
                    string binaryData = Convert.ToBase64String(streamMemory.GetBuffer());
                  
                    fs.Write(binaryData);
                }
                finally
                {
                    
                    fs.Flush();
                    fs.Close();
                }
            }
            catch
            {
                 
            }
        }

        public static object Deserialize(string filePath)
        {
            object data = new object();

            try
            {
             
                StreamReader sr = new StreamReader(filePath);

                try
                {
                    MemoryStream streamMemory;
                    BinaryFormatter formatter = new BinaryFormatter();

                    
                    string cipherData = sr.ReadToEnd();

                
                    byte[] binaryData = Convert.FromBase64String(cipherData);

                    
                    streamMemory = new MemoryStream(binaryData);
                    data = formatter.Deserialize(streamMemory);
                }
                catch
                {
                  
                    data = null;
                }
                finally
                {
                    
                    sr.Close();
                }
            }
            catch
            {
               
                data = null;
            }

            return data;
        }

    }
}
